package org.hongzhisoft.dis.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hongzhisoft.dis.bean.User;
import org.hongzhisoft.dis.service.UserService;
import org.hongzhisoft.dis.service.impl.UserServiceImpl;

/**
 * 登录类
 * @author HongChengzhi
 *
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    
    private UserService userService = new UserServiceImpl();
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String account = request.getParameter("account"); 
        String password = request.getParameter("password"); 
        HttpSession session = request.getSession();
        try {
        	User result = userService.loginUser(account, password);
        	
            if(result != null) {
            	session.setAttribute("loginUser", result);
            	response.sendRedirect(request.getContextPath()+"/");
            }else {
            	request.setAttribute("errorMessage", "您输入的用户名或密码错误，请重试！");
            	request.getRequestDispatcher("/login.jsp").forward(request, response);
			}
            
            // TODO 校验输入栏中的值是否为空，合法性，使用数据库中表字段的长度进行校验（新增，修改）
            // TODO 新增，修改用户时，用户名不能重复的校验,新增，修改角色时，角色权限字符串不能重复的校验
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
    }
    
}
